package com.coda.clouddisk.service.impl;

import com.coda.clouddisk.context.BaseContext;
import com.coda.clouddisk.entity.FriendCategory;
import com.coda.clouddisk.entity.KesunReturn;
import com.coda.clouddisk.entity.User;
import com.coda.clouddisk.mapper.FriendCategoryMapper;
import com.coda.clouddisk.mapper.SuperMapper;
import com.coda.clouddisk.service.SuperService;
import com.coda.clouddisk.service.inter.FriendCategoryService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class FriendCategoryServiceImpl extends SuperService implements FriendCategoryService {
    @Autowired
    private FriendCategoryMapper friendCategoryMapper;

    @Override
    public SuperMapper getMapper() {
        return friendCategoryMapper;
    }


    /**
     * 获取树状结构好友分类
     * @param data
     * @return
     */
    @Override
    public KesunReturn findAll(Map<String, Object> data) {
        KesunReturn kesunReturn = super.findAll(data);
        ArrayList<FriendCategory> obj = (ArrayList<FriendCategory>) kesunReturn.getObj();
        ArrayList<FriendCategory> friendCategoryTree = getFriendCategoryTree(obj,"0");
        kesunReturn.setObj(friendCategoryTree);
        return kesunReturn;
    }

    /**
     * 递归获取树形结构
     */
    private ArrayList<FriendCategory> getFriendCategoryTree(ArrayList<FriendCategory> typeList,String pid){
        //创建集合用户存储类型树
        ArrayList<FriendCategory> treeList = new ArrayList<>();

        //遍历所有类型
        for (FriendCategory friendCategory : typeList) {
            //如果父类id==当前id则添加
            if (friendCategory.getParent().equals(pid)){
                treeList.add(friendCategory);
            }
        }

        //遍历存储类型树的集合
        for (FriendCategory friendCategory : treeList) {
            //递归获取二级菜单并添加
            ArrayList<FriendCategory> friendCategoryTree = getFriendCategoryTree(typeList, friendCategory.getId());
            friendCategory.setChildCategory(friendCategoryTree);
        }
        return treeList;
    }
}
